import config from '../../config'
import banner from '../../models/banner'
import grid from '../../models/grid'
import goods from '../../models/goods'

Page({
  data: {
    inputVal: "", // 搜索框内容

    loadingHidden: false, // loading
    loadingMoreHidden: true,

    banners: [],
    categories: [],
    goodsList: [],
    curPage: 0,
    pageSize: 10,
  },
  tabClick: function(e) {
    wx.navigateTo({
      url: '/pages/goods/list?categoryId=' + e.currentTarget.id,
    })
  },
  toDetailsTap: function(e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  onLoad(e) {
    wx.showShareMenu({
      withShareTicket: true
    }) 
    wx.setNavigationBarTitle({
      title: config.shopName
    })
    this.loadHomeBanner()
    this.loadHomeGrid()
    this.getGoodsList()
  },
  async loadHomeBanner() {
    // const banners = await banner.getHomeBanner(1, 3)
    const banners = [
      {
        businessId: 0,
        id: '1',
        picture: '../../images/index/index1.jpg',
      },
      {
        businessId: 1,
        id: '2',
        picture: '../../images/index/index2.jpg',
      },
      {
        businessId: 2,
        id: '3',
        picture: '../../images/index/index3.jpg',
      },
    ]
    this.setData({
      banners: banners
    });
  },
  async loadHomeGrid(){
    // const data = await grid.getGridList(1, 8)
    const data = [
      {
        id: '1',
        picture: '../../images/icon/next.png',
        name: '运动服饰'
      },
      {
        id: '2',
        picture: '../../images/icon/next.png',
        name: '配饰'
      },
      {
        id: '3',
        picture: '../../images/icon/next.png',
        name: '运动器材'
      },
      {
        id: '4',
        picture: '../../images/icon/next.png',
        name: '佩戴'
      },
      
      
    ]
    const { error_code, msg } = data
    if (error_code !== undefined) {
      console.log(msg)
      return
    }
    this.setData({
      categories: data
    });
  },
  onPageScroll(e) {
  },
  async getGoodsList() {
    wx.showLoading({
      mask: true,
      title: '加载中'
    })
    const curPage = this.data.curPage + 1
    // const res = await goods.getGoodsList('', 0, 0, curPage, this.data.pageSize)
    // const {error_code, msg} = res
    // if (error_code !== undefined) {
    //   console.log(msg)
    //   wx.hideLoading()
    //   return
    // }
    // let goodsList = this.data.goodsList
    // if (curPage === 1) {
    //   goodsList = res.list
    // } else {
    //   goodsList = goodsList.concat(res.list)
    // }
    let goodsList = [
      {
        picture: '../../images/goods/zhihandai.jpg',
        title: '止汗带',
        price: '23',
        humanNum: '50',
      },
      {
        picture: '../../images/goods/jinshenku.jpg',
        title: '紧身裤',
        price: '175',
        humanNum: '33',
      },
      {
        picture: '../../images/goods/duanxiu.jpg',
        title: '短袖',
        price: '123',
        humanNum: '73',
      },
      {
        picture: '../../images/goods/paobuji.jpg',
        title: '跑步机',
        price: '1953',
        humanNum: '2',
      },
      {
        picture: '../../images/goods/yujiadian.jpg',
        title: '瑜伽垫',
        price: '93',
        humanNum: '22',
      },
      {
        picture: '../../images/goods/cheng.jpg',
        title: '体重秤',
        price: '102',
        humanNum: '55',
      },
      {
        picture: '../../images/goods/beibao.jpg',
        title: '背包',
        price: '89',
        humanNum: '43',
      }, {
        picture: '../../images/goods/fadai.jpg',
        title: '发带',
        price: '32',
        humanNum: '221',
      },
      {
        picture: '../../images/goods/yujiaqiu.jpg',
        title: '瑜伽球',
        price: '45',
        humanNum: '3',
      },

    ]
    // const loadingMoreHidden = res.list.length === this.data.pageSize
    this.setData({
      curPage: curPage,
      // loadingMoreHidden: loadingMoreHidden,
      goodsList: goodsList
    });
    wx.hideLoading()
  },
  onShareAppMessage: function() {    
    return {
      title: config.shareProfile,
      path: '/pages/index/index'
    }
  },
  onReachBottom: function() {
    if (!this.data.loadingMoreHidden) {
      return
    }
    this.getGoodsList()
  },
  onPullDownRefresh: function() {
    this.setData({
      curPage: 0
    });
    // this.getGoodsList()
    wx.stopPullDownRefresh()
  },
  bindinput(e) {
    this.setData({
      inputVal: e.detail.value
    })
  },
  bindconfirm(e) {
    this.setData({
      inputVal: e.detail.value
    })
    wx.navigateTo({
      url: '/pages/goods/list?k=' + this.data.inputVal,
    })
  },
})